<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form :label-col="{ md: { span: 6 }, sm: { span: 24 } }" :wrapper-col="{ md: { span: 18 }, sm: { span: 24 } }">
        <a-row>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="名称">
              <!-- <a-input v-model:value.trim="sellSonWhere.name" placeholder="请输入" @keyup.enter="sellSonReload" @change="sellSonReload" allow-clear />-->
              <a-input v-model:value.trim="sellSonWhere.name" placeholder="请输入名称" allow-clear />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="选择">
              <a-select v-model:value="sellSonWhere.sex" placeholder="请选择" allow-clear>
                <a-select-option value="1">1</a-select-option>
                <a-select-option value="2">2</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{ span: 24 }">
              <em></em>
              <a-space>
                <a-button type="primary" @click="sellSonReload">查询</a-button>
                <a-button @click="sellSonReset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table ref="sellSonTableRef" row-key="id" :columns="columns" :datasource="datasource" :scroll="{ x: 1000 }" cacheKey="sellSon">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="sellSonOpenEdit()" v-any-permission="['store:sellSon:save']">
              <template #icon>
                <plus-outlined />
              </template>
              <span>添加</span>
            </a-button>
          </a-space>
        </template>
        <template #bodyCell="{ column, record }">
          <template v-if="column.key === 'action'">
            <a-space>
              <a @click="sellSonOpenEdit(record)" v-any-permission="['store:sellSon:update']">修改</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定要删除此项吗？" @confirm="sellSonRemove(record)">
                <a class="ele-text-danger" v-any-permission="['store:sellSon:remove']">删除</a>
              </a-popconfirm>
            </a-space>
          </template>
        </template>
      </ele-pro-table>
    </a-card>
    <!-- 销售单子表添加、修改弹窗 -->
    <ele-modal :width="460" v-model:visible="sellSonVisible_modal" :confirm-loading="loading" :title="sellSonIsUpdate ? '修改销售单子表' : '添加销售单子表'"
      position="center" :body-style="{ paddingBottom: '8px' }" @ok="sellSonSave" :maskClosable="false" >
      <a-form ref="sellSonFormRef" :model="sellSonForm" :rules="sellSonRules" :label-col="{ md: { span: 7 }, sm: { span: 24 } }"
        :wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }">
        <a-row :gutter="16">
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="we_sell主表id" name="sellid">
              <a-input allow-clear :maxlength="200" placeholder="请输入we_sell主表id" v-model:value="sellSonForm.sellid" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="仓库" name="warehousename">
              <a-input allow-clear :maxlength="200" placeholder="请输入仓库" v-model:value="sellSonForm.warehousename" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="产品" name="productname">
              <a-input allow-clear :maxlength="200" placeholder="请输入产品" v-model:value="sellSonForm.productname" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="规格" name="specification">
              <a-input allow-clear :maxlength="200" placeholder="请输入规格" v-model:value="sellSonForm.specification" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="长度" name="lengths">
              <a-input allow-clear :maxlength="200" placeholder="请输入长度" v-model:value="sellSonForm.lengths" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="负差" name="negative">
              <a-input allow-clear :maxlength="200" placeholder="请输入负差" v-model:value="sellSonForm.negative" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="数量" name="num">
              <a-input allow-clear :maxlength="200" placeholder="请输入数量" v-model:value="sellSonForm.num" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="理论质量" name="liweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入理论质量" v-model:value="sellSonForm.liweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="总重量" name="litotalweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入总重量" v-model:value="sellSonForm.litotalweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="销售重量" name="actualweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入销售重量" v-model:value="sellSonForm.actualweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="实际总重量" name="actualtotalweight">
              <a-input allow-clear :maxlength="200" placeholder="请输入实际总重量" v-model:value="sellSonForm.actualtotalweight" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="价格" name="price">
              <a-input allow-clear :maxlength="200" placeholder="请输入价格" v-model:value="sellSonForm.price" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="总价" name="totalprice">
              <a-input allow-clear :maxlength="200" placeholder="请输入总价" v-model:value="sellSonForm.totalprice" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="材质" name="model">
              <a-input allow-clear :maxlength="200" placeholder="请输入材质" v-model:value="sellSonForm.model" autocomplete="off" />
            </a-form-item>
          </a-col>
          <a-col :md="24" :sm="24" :xs="24">
            <a-form-item label="产地" name="locality">
              <a-input allow-clear :maxlength="200" placeholder="请输入产地" v-model:value="sellSonForm.locality" autocomplete="off" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </ele-modal>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue';
import { toDateString } from 'ele-admin-pro';
import useFormData from '@/utils/use-form-data';
import { message } from 'ant-design-vue';
import { TIME_OUT } from '@/config/damon';

import { pageSellSon, removeSellSon, addSellSon, updateSellSon } from '@/api/store/sellSon';

// 表格实例
const sellSonTableRef = ref(null);

// 表格列配置
const columns = ref([
  {
    key: 'index',
    width: 48,
    align: 'center',
    fixed: 'left',
    hideInSetting: true,
    customRender: ({ index }) => index + (sellSonTableRef.value?.tableIndex ?? 0)
  },
  {
    title: 'we_sell主表id',
    dataIndex: 'sellid',
    key: 'sellid',
    sorter: true
  },
  {
    title: '仓库',
    dataIndex: 'warehousename',
    key: 'warehousename',
    sorter: true
  },
  {
    title: '产品',
    dataIndex: 'productname',
    key: 'productname',
    sorter: true
  },
  {
    title: '规格',
    dataIndex: 'specification',
    key: 'specification',
    sorter: true
  },
  {
    title: '长度',
    dataIndex: 'lengths',
    key: 'lengths',
    sorter: true
  },
  {
    title: '负差',
    dataIndex: 'negative',
    key: 'negative',
    sorter: true
  },
  {
    title: '数量',
    dataIndex: 'num',
    key: 'num',
    sorter: true
  },
  {
    title: '理论质量',
    dataIndex: 'liweight',
    key: 'liweight',
    sorter: true
  },
  {
    title: '总重量',
    dataIndex: 'litotalweight',
    key: 'litotalweight',
    sorter: true
  },
  {
    title: '销售重量',
    dataIndex: 'actualweight',
    key: 'actualweight',
    sorter: true
  },
  {
    title: '实际总重量',
    dataIndex: 'actualtotalweight',
    key: 'actualtotalweight',
    sorter: true
  },
  {
    title: '价格',
    dataIndex: 'price',
    key: 'price',
    sorter: true
  },
  {
    title: '总价',
    dataIndex: 'totalprice',
    key: 'totalprice',
    sorter: true
  },
  {
    title: '材质',
    dataIndex: 'model',
    key: 'model',
    sorter: true
  },
  {
    title: '产地',
    dataIndex: 'locality',
    key: 'locality',
    sorter: true
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    key: 'createTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '创建人',
    dataIndex: 'createName',
    key: 'createName',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    key: 'updateTime',
    resizable: true,
    width: 150,
    ellipsis: true,
    customRender: ({ text }) => toDateString(text)
  },
  {
    title: '更新人',
    dataIndex: 'updateName',
    key: 'updateName',
  },
  {
    title: '操作',
    key: 'action',
    width: 200,
    align: 'center',
    fixed: 'right',
    hideInSetting: true
  }
]);

// 表单数据
const { form: sellSonWhere, resetFields } = useFormData({
  username: '',
  sex: undefined
});

// 表格数据源
const datasource = async ({ page, limit, where, orders }) => {
  let data_temp = {}
  orders.sort = orders.sort=='updateTime'? 'update_time':orders.sort
  orders.sort = orders.sort=='createTime'? 'create_time':orders.sort
  await pageSellSon({ ...where, ...orders, page, limit }).then(data=>{
    data_temp = data.data
  })
  return new Promise((resolve) => {
    resolve(data_temp)
  })

};

/* 搜索 */
const sellSonReload = () => {
  sellSonTableRef?.value?.reload({ page: 1, where: sellSonWhere });
};
const sellSonReload2 = () => {
  sellSonTableRef?.value?.reload();
};

let sellSonWhere_timeout = 0
watch(() => sellSonWhere, (val) => {
  clearTimeout(sellSonWhere_timeout)
  sellSonWhere_timeout = setTimeout(() => {
    sellSonReload();
  }, TIME_OUT);
},
{ deep: true }
)

/*  重置 */
const sellSonReset = () => {
  resetFields();
  sellSonReload();
};

// 当前选中数据
const sellSonCurrent = ref(null);

// 弹窗是否打开
const sellSonVisible_modal = ref(false);

// 是否是修改
const sellSonIsUpdate = ref(false);

// 提交状态
const loading = ref(false);

//
const sellSonFormRef = ref(null);

// 表单数据
const { form: sellSonForm, resetFields: sellSonResetFormFields } = useFormData({
  id: undefined,
  customer: '',
  vessel: '',
});

// 表单验证规则
const sellSonRules = reactive({
  name: [
    {
      required: true,
      message: '请输入名称',
      type: 'string',
    }
  ],
});

/* 打开编辑弹窗 */
const sellSonOpenEdit = (record) => {
  sellSonResetFormFields();
  sellSonFormRef.value?.clearValidate();
  sellSonCurrent.value = record?? {};
  sellSonVisible_modal.value = true;
  Object.assign(sellSonForm, record ?? {});
  sellSonIsUpdate.value = !!record;
};

/* 保存编辑 */
const sellSonSave = () => {
  if (!sellSonFormRef.value) {
    return;
  }
  sellSonFormRef.value.validate().then(() => {
    loading.value = true;
    const saveOrUpdate = sellSonIsUpdate.value ? updateSellSon : addSellSon;
    saveOrUpdate(sellSonForm).then((msg) => {
      loading.value = false;
      message.success(msg);
      sellSonVisible_modal.value = false;
      sellSonReload2();
    }).catch((e) => {
      loading.value = false;
      message.error(e.message);
    });
  }).catch(() => {
  });
};

/* 删除单个 */
const sellSonRemove = (record) => {
  const hide = message.loading('请求中..', 0);
  removeSellSon(record.id).then((msg) => {
    hide();
    message.success(msg);
    sellSonReload2();
  }).catch((e) => {
    hide();
    message.error(e.message);
  });
};
</script>

<style lang="less" scoped>
@import "@/assets/css/public.less";

.re {
  background-color: @button-pay;
}

</style>
